package com.jxmcloud.business.cut.controller.nah.login;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import com.jxmcloud.business.cut.service.user.IUserManagerService;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.util.RedissonLockUtil;
import com.jxmcloud.business.cut.constants.CutRedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RedissonClient;
import com.jxmcloud.business.cut.service.user.IUserActivityService;


import com.jxmcloud.business.cut.service.nah.login.ICutLoginService;
import com.jxmcloud.business.cut.dto.client.LoginFromXLDto;
import com.jxmcloud.business.cut.vo.client.LoginFromXLVo;


@Validated
@Slf4j
@RestController
@RequestMapping("/nah/login/CutLoginController")
public class CutLoginController {
    @Resource
    private IUserManagerService userManagerService;
    @Resource
    private IUserActivityService userActivityService;
    @Autowired
    private RedissonClient redissonClient;
    @Resource
    private ICutLoginService cutLoginService;


    // 玄乐登录
    @PostMapping("/loginFromXL")
    public String loginFromXL(@RequestBody @Valid LoginFromXLDto dto) {
        Integer uid = 0;
        try {
            LoginFromXLVo data = new RedissonLockUtil(redissonClient, CutRedisConstant.cutLoginLock(dto.getAccount())).lock(() ->
                    cutLoginService.loginFromXL(dto, null));
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }
}
